<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.sql.Connection.*" %>
<%@ page import="java.sql.ResultSet.*" %>
<%@ page import="java.sql.SQLException.*" %>
<%@ page import="java.sql.Statement.*" %>
<%@ page import="java.util.ArrayList.*" %>
<%@ page import="java.util.Collection.*" %>
<%@ page import="java.util.Iterator.*" %>
<%@ page import="org.apache.commons.logging.Log.*" %>
<%@ page import="org.apache.commons.logging.LogFactory.*" %>
<%@ page import="org.exolab.castor.jdo.Database.*" %>
<%@ page import="org.exolab.castor.jdo.PersistenceException.*" %>
<%@ page import="org.mbari.vars.dao.IDataObject.*" %>
<%@ page import="org.mbari.vars.dao.ObjectDAO.*" %>
<%@ page import="org.mbari.vars.knowledgebase.model.*" %>
<%@ page import="org.mbari.vars.knowledgebase.model.Concept.*" %>
<%@ page import="org.mbari.vars.knowledgebase.model.ConceptDelegate.*" %>
<%@ page import="org.mbari.vars.knowledgebase.model.ConceptName.*" %>
<%@ page import="org.mbari.vars.knowledgebase.model.dao.*" %>
<%@ page import="org.mbari.vars.dao.*" %>

<% 
  StringBuffer thisStep = new StringBuffer( );
  String nameToFind = request.getParameter( "nameToFind" );
  if ( nameToFind != null ) {
    if ( nameToFind.equals( "null" ) ) {
      nameToFind = null;
    }
  }
  String nameToOpen = request.getParameter( "nameToOpen" );
  if ( nameToOpen != null ) {
    if ( nameToOpen.equals( "null" ) ) {
      nameToOpen = null;
    }
  }

  String startOfOldList = (String) session.getAttribute( "startOfOldList" );
  if ( startOfOldList != null ) {
    if ( startOfOldList.equals( "null" ) ) {
      startOfOldList = null;
    }
  }

  String oldList = (String) session.getAttribute( "oldList" );
  if ( oldList != null ) {
    if ( oldList.equals( "null" ) ) {
      oldList = null;
    }
  }
  session.removeAttribute( "oldList" );
%>

<head>
<SCRIPT SRC="mktree.js" LANGUAGE="JavaScript"></SCRIPT> 
<LINK REL="stylesheet" HREF="mktree.css">
</head>
<html>
<body>
<A href="#" onClick="expandTree('tree1'); return false;">Expand All</A>&nbsp;&nbsp;&nbsp;
<A href="#" onClick="collapseTree('tree1'); return false;">Collapse All</A>&nbsp;&nbsp;&nbsp;
<%= nameToOpen == null ? "" : "<A href=\"#\" onClick=\"expandToItem('tree1','" + nameToOpen.replaceAll( "\"", "" ) + "'); return false;\">Expand and find " + nameToOpen.replaceAll( "\"", "" ) + "</A>" %>



<%!
 public String recurseKids (Concept parent, String pName, int level) {
   Collection<Concept> children = parent.getChildConceptColl();
   if ( children.isEmpty() )
     return "";

   StringBuffer thisStep = new StringBuffer( "\r\n<ul>" );
   for ( Concept child : children ) {
     String primaryName = child.getPrimaryConceptNameAsString();
     /*
     String[] names = child.getConceptNamesAsStrings();
     for ( int i = 0; i < names.length; i++ ) {
       thisStep.append( "<li id=\"" + names[i] + "\">" + names[i] + "</li>\r\n" );
     }
     */
     thisStep.append( "<li id=\"" + primaryName + "\">" + primaryName + "</li>\r\n" );
     if ( level < 1 ) {
       thisStep.append( recurseKids( child, primaryName, level + 1 ) );
     }
   }
   thisStep.append( "</ul>\r\n" );
   return thisStep.toString();
 }
%>
<%
   thisStep = new StringBuffer( );
   try {
     Concept concept = null;
     // Use a DAO Object to fetch a concept by name
     if ( nameToFind == null ) {
       if ( nameToOpen == null ) {
         concept = ConceptDAO.getInstance().findRoot();
       } else {
         concept = ConceptDAO.getInstance().findByName( nameToOpen.replaceAll( "\"", "" ) );
       }
     } else {
       concept = ConceptDAO.getInstance().findByName( nameToFind.replaceAll( "\"", "" ) );
     }
     if (concept != null) {

       Concept parentConcept = concept.getParentConcept();
       String gpName = null;
       if ( parentConcept != null ) {
         gpName = parentConcept.getPrimaryConceptNameAsString( );
       }
       if ( nameToFind == null ) {
         gpName = (String) session.getAttribute( "gpName" );
       } else {
         if ( gpName != null ) {
           session.setAttribute( "gpName", gpName );
         } else {
           session.removeAttribute( "gpName" );
         }
       }

       String pName = concept.getPrimaryConceptNameAsString( );
       String[] names = concept.getConceptNamesAsStrings();

       thisStep.append( recurseKids( concept, pName, 1 ) );

       if ( nameToFind != null ) {
         if ( oldList != null ) {
           int insertAt = thisStep.toString().indexOf( startOfOldList );
           if ( insertAt >= 0) {
/*
             %><br><%= "\r\n" + startOfOldList %>&nbsp;found at <%= insertAt %> in<%
             %><br><%= "\r\n" + thisStep.toString() %><%
*/
             thisStep.insert( insertAt + startOfOldList.length() + 1, oldList.replaceAll( startOfOldList, "") );
           } else {
             if ( startOfOldList != null ) {
               %><br><%= "\r\n" + startOfOldList + " " + insertAt %> not found in<%
               %><%= oldList %><%
             } else {
               %>oldList not found<%
             }
           }
         }
       } else
       if ( nameToOpen != null ) {
         String startOfNewList = "<li id=" + nameToOpen + ">" + nameToOpen.replaceAll( "\"", "") + "</li>";
         if ( oldList != null ) {
           StringBuffer bOldList = new StringBuffer( startOfOldList + oldList );
           int insertAt = bOldList.toString().indexOf( startOfNewList );
           if ( insertAt > 0) {
             if ( bOldList.toString().indexOf( thisStep.toString() ) < 0 ) {
               bOldList.insert( insertAt + startOfNewList.length(), thisStep.toString() );
             }
             thisStep = bOldList;
           } else{
             %><br><%= "\r\n" + startOfNewList + " " + insertAt %> not found in<%
             %><br><%= "\r\n" + bOldList.toString() %><%
           }
         } else {
           %>oldList not found<%
         }
       }
       if ( gpName != null ) {
         %><br><br><a href=MBARI.jsp?nameToFind=<%= gpName %>><small>go up</small></a><%
       } else {
         %><br>What happened to Gramps?<%
       }   
       %><%= "\r\n" %><UL CLASS="mktree" id="tree1"><%

       startOfOldList = "<li id=\"" + pName + "\">" + pName + "</li>";
       if ( nameToFind == null ) {
         startOfOldList = (String) session.getAttribute( "startOfOldList" );
       } else {
         if ( pName != null ) {
           session.setAttribute( "startOfOldList", startOfOldList );
         } else {
           session.removeAttribute( "startOfOldList" );
         }
       }

       if ( thisStep.toString().indexOf( startOfOldList ) < 0 ) {
         %><%= "\r\n<br>" + startOfOldList %><%
       }
       %><%= "\r\n" %><%= thisStep.toString() %><%
       %></UL><%
       if ( thisStep != null ) {
         %><select onChange=location=this.options[this.selectedIndex].value;>
           <%= thisStep.toString().replaceAll( "<ul>", "" ).replaceAll( "</ul>", "" ).replaceAll( "<li", "      <option" ).replaceAll( "</li>", "</option>" ).replaceAll( " id=", " value=MBARI.jsp?nameToOpen=" ) %>
         </select><%
       }
       startOfOldList = "<li id=\"" + pName + "\">" + pName + "</li>";
       //session.setAttribute( "startOfOldList", startOfOldList );
       session.setAttribute( "oldList", thisStep.toString() );
     }
     // Get all the names for the concept as ConceptName objects
     Collection<ConceptName> names = concept.getConceptNameColl();
     // Get all the names for the concept as Strings
     String[] names2 = concept.getConceptNamesAsStrings();
     //for ( int i=0; i< names2.length; i++ )
     //  System.out.println( names2[i] );
     // Get the primary name as a ConceptName 
     ConceptName primaryName = concept.getPrimaryConceptName();
     // Get the primary name as a string
     String primaryName2 = concept.getPrimaryConceptNameAsString();
     // or you could do: String primaryName2 =  primaryName.getName();
     // Get descriptive info. We'll talk more about this in the future
     Set<LinkRealization> descInfo = concept.getLinkRealizationSet();
     System.out.println( "Finished " + (new java.util.Date( System.currentTimeMillis() ) ).toString() );
   } catch (Exception ex) {
     %><%= "\r\n\r\n" %><br><br><font color=red size=4>Test Error!&nbsp;<%
     %><%= ex.toString() %></font><br>
       <%= "\r\nLooking for " + nameToFind + " in" %>
       <%= "\r\n\r\n" + oldList %><%
   }
%>

</body>
</html>
